<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Address extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->check_authentication(2);
		$this->load->model('Company_address_model', 'address');
		$this->lang->load('company/address', $this->session->userdata['user_lang']);
	}
	
	public function index()
	{
		$address_list = $this->address->get(array('idcompany' => $this->session->userdata['idprofile']));
				
		$grid_address = array();
        
        foreach ($address_list->result() as $row)
		{
			$grid_address[] = '<div class="row"><span><span class="gtitle">' . $row->address . ', '.$row->number . ($row->additional != '' ? (' ('.$row->additional.')') : '') . '</span>'
			.'<br/><span class="gdesc">'.$row->neighborhood.' - ' . $row->city .' / '.$row->stateshortname . ' (' . $row->countryname . ')' 
			. '</span></span>
			<div class="gtool"><a onclick="CompanyAddress.editAddress('.$row->id.');">'.$this->lang->line('global_update').'</a>
			 &middot; <a onclick="CompanyAddress.deleteAddress('.$row->id.');">'.$this->lang->line('global_delete').'</a></div></div>';
		}
		
		if (!$grid_address) {
			 $data['grid_address'] = '<center>'.$this->lang->line('address_noresults').'</center>';
		} else {
			 $data['grid_address'] = implode('<hr/>', $grid_address);
		}
		
		exitHTML($data['grid_address']);
	}
	
	public function insert()
	{
		$data = $_POST;
		
		if (missing_field($data, array('idcountry', 'city', 'address')))
		{
			formError($this->lang->line('address_missingfield'), true);
		}
		
		if (isset($data['number']) && !is_numeric($data['number']))
		{
			formError($this->lang->line('address_invalidnumber'), true);
		}
		
		$data['idcompany'] = $this->session->userdata['idprofile'];
		
		if ($this->address->insert($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true);
		}
		else
		{
			exitJs('CompanyAddress.getAddressGrid();closeBox();');
		}
	}
	
	public function update($id_address = '')
	{
		$data = $_POST;
		
		if (missing_field($data, array('idcountry', 'city', 'address')))
		{
			formError($this->lang->line('address_missingfield'), true);
		}
		
		if (isset($data['number']) && !is_numeric($data['number']))
		{
			formError($this->lang->line('address_invalidnumber'), true);
		}
		
		$data['idcompany'] = $this->session->userdata['idprofile'];
		
		if ($this->address->update($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true);
		}
		else
		{
			exitJs('CompanyAddress.getAddressGrid();closeBox();');
		}
	}
	
	public function delete()
	{
		$data = $_POST;
		
		if (missing_field($data, array('id')))
		{
			formError($this->lang->line('address_missingfield'));
		}
		
		if ($this->address->delete(array('id' => $data['id'])) === false)
		{
		   formError($this->lang->line('global_systemfail'));
		}
		else
		{
			exitJs('CompanyAddress.getAddressGrid();');
		}
	}
	
	public function edit($id_address = '')
	{
		header("Content-type: text/html; charset=utf-8");
		$data = $_POST;
		
		$data['id_address'] = $id_address;
		$data['zipcode'] = '';
		$data['city'] = '';
		$data['neighborhood'] = '';
		$data['address'] = '';
		$data['number'] = '';
		$data['additional'] = '';
		
		if (is_numeric($id_address))
		{
			$address_data = $this->address->get(array('idaddress' => $id_address))->row();
			
			$data['extrascript']  = '$("#zipcode").val("'.$address_data->zipcode.'");';
			$data['extrascript'] .= '$("#city").val("'.$address_data->city.'");';
			$data['extrascript'] .= '$("#neighborhood").val("'.$address_data->neighborhood.'");';
			$data['extrascript'] .= '$("#address").val("'.$address_data->address.'");';
			$data['extrascript'] .= '$("#number").val("'.$address_data->number.'");';
			$data['extrascript'] .= '$("#additional").val("'.$address_data->additional.'");';
			$data['extrascript'] .= '$("#idcountry").val("'.$address_data->idcountry.'");';
			$data['extrascript'] .= '$("#idstate").val("'.$address_data->idstate.'");';
		}
		
		$data['userdata'] = $this->session->userdata;
        $this->load->view('company/address', $data);
	}
	
	public function add()
	{
		$this->edit();
	}
}

?>